package chat.aylan.top.client;

import chat.aylan.top.CloseUtil;

import java.io.DataInputStream;
import java.io.IOException;
import java.net.Socket;

public class ReceiverThread implements Runnable {

    private DataInputStream dis;
    private boolean isRunning = true;

    public ReceiverThread(Socket socket){
        try {
            dis = new DataInputStream(socket.getInputStream());
        } catch (IOException e) {
            die();
        }
    }

    private String listen(){
        String str = "";
        try {
            str = dis.readUTF();
        } catch (IOException e) {
           die();
        }
        return str;
    }


    private void show(){
        System.out.println(listen());
    }

    @Override
    public void run() {
        while (isRunning){
            show();
        }
    }

    private void die(){
        isRunning = false;
        CloseUtil.closeAll(dis);
    }
}
